using System;
using System.Collections.Generic;
using System.Linq;

public class SupplierItem
{
    public string Id { get; set; }
    public string SupplierPartId { get; set; }
    public string Name { get; set; }
    public string UnitPriceStr { get; set; }
    public string BrandId { get; set; }
    public string CatId { get; set; }
    public string Status { get; set; }
    public string Cycle { get; set; }
    public string Sku { get; set; }
    public string Desc { get; set; }
    public string StoreCondition { get; set; }
    public string Purity { get; set; }
    public string ExtImageUrl { get; set; }
    public string Cas { get; set; }
    public string Specifications { get; set; }
    public string ItemDetail { get; set; }
    public string MarketPriceStr { get; set; }
}

public class SupplierResponse
{
    public List<string> SuccessIds { get; set; }
    public List<string> ErrorMessages { get; set; }
}

public class SupplierService
{
    private List<SupplierItem> _shpolist = new List<SupplierItem>();

    public SupplierResponse ProcessItems(List<SupplierItem> itemList)
    {
        var response = new SupplierResponse
        {
            SuccessIds = new List<string>(),
            ErrorMessages = new List<string>()
        };

        foreach (var item in itemList)
        {
            try
            {
                if (string.IsNullOrEmpty(item.MarketPriceStr))
                {
                    response.ErrorMessages.Add($"Item with SupplierPartId {item.SupplierPartId} has no market price.");
                    continue;
                }

                if (!string.IsNullOrEmpty(item.Id))
                {
                    // Update existing item
                    var existingItem = _shpolist.FirstOrDefault(i => i.Id == item.Id);
                    if (existingItem != null)
                    {
                        existingItem.SupplierPartId = item.SupplierPartId;
                        existingItem.Name = item.Name;
                        existingItem.UnitPriceStr = item.UnitPriceStr;
                        existingItem.BrandId = item.BrandId;
                        existingItem.CatId = item.CatId;
                        existingItem.Status = item.Status;
                        existingItem.Cycle = item.Cycle;
                        existingItem.Sku = item.Sku;
                        existingItem.Desc = item.Desc;
                        existingItem.StoreCondition = item.StoreCondition;
                        existingItem.Purity = item.Purity;
                        existingItem.ExtImageUrl = item.ExtImageUrl;
                        existingItem.Cas = item.Cas;
                        existingItem.Specifications = item.Specifications;
                        existingItem.ItemDetail = item.ItemDetail;
                        existingItem.MarketPriceStr = item.MarketPriceStr;

                        response.SuccessIds.Add(item.Id);
                    }
                    else
                    {
                        response.ErrorMessages.Add($"Item with Id {item.Id} not found.");
                    }
                }
                else
                {
                    // Add new item
                    var newItem = new SupplierItem
                    {
                        SupplierPartId = item.SupplierPartId,
                        Name = item.Name,
                        UnitPriceStr = item.UnitPriceStr,
                        BrandId = item.BrandId,
                        CatId = item.CatId,
                        Status = item.Status,
                        Cycle = item.Cycle,
                        Sku = item.Sku,
                        Desc = item.Desc,
                        StoreCondition = item.StoreCondition,
                        Purity = item.Purity,
                        ExtImageUrl = item.ExtImageUrl,
                        Cas = item.Cas,
                        Specifications = item.Specifications,
                        ItemDetail = item.ItemDetail,
                        MarketPriceStr = item.MarketPriceStr
                    };

                    newItem.Id = Guid.NewGuid().ToString(); // Generate a new unique ID
                    _shpolist.Add(newItem);

                    response.SuccessIds.Add(newItem.Id);
                }
            }
            catch (Exception ex)
            {
                response.ErrorMessages.Add($"Error processing item with SupplierPartId {item.SupplierPartId}: {ex.Message}");
            }
        }

        return response;
    }
}